package com.miao.shop.controller;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Customer;
import com.miao.common.utils.TelCodeUtil;
import com.miao.shop.entity.vo.LoginVO;
import com.miao.shop.entity.vo.RegisterVO;
import com.miao.shop.entity.vo.RestPasswordVO;
import com.miao.shop.service.ShopCustomerService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


/**
 * 顾客信息表(Customer)表控制层
 *
 * @author Miao WenQing
 * @since 2021-02-14 16:08:26
 */
@RestController
@RequestMapping("common/customer")
public class ShopCustomerController {
    /**
     * 服务对象
     */
    @Resource
    private ShopCustomerService shopCustomerService;


    @Resource
    private TelCodeUtil telCodeUtil;


    /**
     * 登录
     * @param loginVO 登录表单
     * @return R
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO){
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("cus_tel",loginVO.getCusTel());
        Customer customer = this.shopCustomerService.getOne(customerQueryWrapper);
        if ( null==customer){
            return R.error("账号不存在");
        }else {
           if ( new BCryptPasswordEncoder().matches(loginVO.getCusPasswd(), customer.getCusPasswd())){
               customer.setCusPasswd(null);
               return  R.ok(customer);
           }else {
               return R.error("密码错误");
           }
        }
    }


    /**
     * 账号注册
     * @param registerVO 提交表单
     * @return R
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO){


        if (!telCodeUtil.checkTelCode(registerVO.getTel(),registerVO.getCode())) {
            return R.error("验证码错误");
        }
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("cus_tel",registerVO.getTel());
        Customer customer = this.shopCustomerService.getOne(customerQueryWrapper);

        if (null!=customer){
            return R.error("该手机号已注册");
        }else {
            customer = new Customer();
            customer.setCusPasswd(new BCryptPasswordEncoder().encode(registerVO.getPasswd()));
            customer.setCusTel(registerVO.getTel());
            if (this.shopCustomerService.save(customer)) {
                return R.ok();
            }else {
                return R.error();
            }

        }


    }


    /**
     * 修改用户信息
     * @param customer 用户
     * @return R
     */
    @PostMapping("/edit")
    public R edit(@RequestBody Customer customer){

        Customer customer1 = this.shopCustomerService.getById(customer.getCusId());
        customer1.setCusGender(customer.getCusGender());
        customer1.setCusNickname(customer.getCusNickname());
        if (this.shopCustomerService.saveOrUpdate(customer1)) {
            return R.ok();
        }else {
            return R.error();
        }

    }

    /**
     *
     * @param cusId 用户id
     * @param avatarUrl 头像链接
     * @return R
     */
    @GetMapping("/editAvatar")
    public R editAvatar(String cusId,String avatarUrl){
        Customer customer = this.shopCustomerService.getById(cusId);
        customer.setCusAvatar(avatarUrl);
        if (this.shopCustomerService.updateById(customer)) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 重置密码
     * @param restPasswordVO 修改密码表单
     * @return R
     */
    @PostMapping("/restPwd")
    public R restPassword(@RequestBody RestPasswordVO restPasswordVO){

        if (!telCodeUtil.checkTelCode(restPasswordVO.getTel(),restPasswordVO.getCode())) {
            return R.error("验证码错误");
        }
        Customer customer = this.shopCustomerService.getById(restPasswordVO.getCusId());

        if (null==customer){
            return R.error("账号不存在");
        }else {
            customer.setCusPasswd(new BCryptPasswordEncoder().encode(restPasswordVO.getPasswd()));
            if (this.shopCustomerService.saveOrUpdate(customer)) {
                return R.ok();
            }else {
                return R.error();
            }

        }

    }






}
